package com.cefa.edu.dao;

import com.cefa.edu.datasource.DataSource;
import com.cefa.edu.model.Course;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author huangzehui
 * @date 18-7-30 下午4:14
 */
@Mapper
public interface CourseMapper {
    @DataSource("cefaedu")
    @Insert({"insert into cefa_edu_course(name,level_id,recordName) values(#{name},#{levelId},#{recordName})"})
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertCourse(Course course);


    @Select("select c.id,c.name,l2.name as levelName2,l3.name as level_name1 from  cefa_edu_course c left join cefa_edu_education_level l on c.level_id=l.id   left join cefa_edu_education_level l2 on l.p_id=l2.id   left join cefa_edu_education_level l3 on l2.p_id=l3.id where c.level_id=#{levelId} and c.status=0")
    @Results(id = "courseMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "levelId", column = "level_name"),
            @Result(property = "recordName", column = "record_name"),

            @Result(property = "levelName1", column = "level_name1"),
            @Result(property = "levelName2", column = "level_name2")
    })
    List<Course> findCourseListByLevelId(@Param("levelId") Integer levelId);


    @Update({"update cefa_edu_course set state = 1 where id = #{courseId}"})
    int updateCourseStatus(@Param("courseId") Integer courseId);

    @Select("select * from \n" +
            "(select * from cefa_edu_course where level_id=(\n" +
            "select id from cefa_edu_education_level where id=(\n" +
            "select p_id from cefa_edu_education_level where id=(\n" +
            "select level_id from cefa_edu_user_education_level where user_id=#{user_id} and state=0 and role_type=6))))\n" +
            "a where state=0")
    @Results({
            @Result(property = "levelId", column = "level_id"),
            @Result(property = "recordName", column = "record_name")
    })
    List<Course> findCourseByUserId(@Param("user_id") Integer user_id);

    @Select("select id, name,level_id,record_name,state from cefa_edu_course where state=0 and id=#{id}")
    @ResultMap(value="courseMap")
    Course getCourseById(@Param("id") Integer id);

    /*=======================================上面是之前写的sql语句 20180815====================================================*/
    /**
     * @aa 添加科目数据
     * @aa add by chaijian 2018-08-15
     */
    @Insert({"INSERT into cefa_edu_course(recording_name,course_name,grade_id,school_id)values(#{recordNames},#{courseName},#{gradeId},#{schoolId})"})
    int saveCourse(Map<String,Object> paramsMap);

    /**
     * @aa 根据班级id 删除班级
     * @aa add by chaijian 2018-08-15
     * @param courseId
     * @return
     */
    @Update({"update cefa_edu_course set state=1 where id=#{courseId}"})
    void deleteCourse(Integer courseId);

    /**
     * @aa 根据科目id 获取对应的班级数据
     * @aa add by chaijian 2018-08-15
     * @return
     */
    @Select({"select c.* from cefa_edu_course c where c.state=0 and c.id=#{courseId}"})
    Map<String,Object> getCourseById2(Integer courseId);

    @Update({"update cefa_edu_course set course_name=#{courseName},recording_name=#{recordingName} where id=#{id}"})
    int  updateCourse(com.cefa.edu.pojo.Course course);

    @Select({"select count(c.id) from cefa_edu_course c where c.course_name=#{courseName} and c.grade_id=#{gradeId} and c.school_id=#{schoolId}"})
    int isExistedOfTheGradeCourse(@Param("schoolId") Integer schoolId, @Param("gradeId")Integer gradeId, @Param("courseName")String courseName);
}
